package io.zt.dangyuan.expandable;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import org.xutils.ex.HttpException;

import io.zt.dangyuan.BaseActivity;
import io.zt.dangyuan.R;
import io.zt.dangyuan.WebUtils;
import io.zt.dangyuan.object.GroupModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2015/7/12.
 */
public class CantactActivity extends BaseActivity implements WebUtils.OnWebListenr{
    List<NLevelItem> list;
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addCenterView(R.layout.activity_contact);
        setTitle("通讯录");
        listView = (ListView) findViewById(R.id.listView1);
        initDatas();
    }
    private void initDatas()
    {
        webRequest(0);
        list = new ArrayList<NLevelItem>();
        Random rng = new Random();
        final LayoutInflater inflater = LayoutInflater.from(this);
        for (int i = 0; i < 5; i++) {
            final NLevelItem grandParent = new NLevelItem(new SomeObject("GrandParent "+i),null, new NLevelView() {
                @Override
                public View getView(NLevelItem item) {
                    View view = inflater.inflate(R.layout.contact_group, null);
                    TextView tv = (TextView) view.findViewById(R.id.group_name);
                    view.setBackgroundColor(Color.GREEN);
                    String name = (String) ((SomeObject) item.getWrappedObject()).getName();
                    tv.setText(name);
                    return view;
                }
            });
            list.add(grandParent);
            int numChildren = rng.nextInt(4) + 1;
            for (int j = 0; j < numChildren; j++) {
                NLevelItem parent = new NLevelItem(new SomeObject("Parent "+j),grandParent, new NLevelView() {

                    @Override
                    public View getView(NLevelItem item) {
                        View view = inflater.inflate(R.layout.contact_item, null);
                        TextView tv = (TextView) view.findViewById(R.id.textView);
                        tv.setBackgroundColor(Color.YELLOW);
                        String name = (String) ((SomeObject) item.getWrappedObject()).getName();
                        tv.setText(name);
                        return view;
                    }
                });

                list.add(parent);
                int grandChildren = rng.nextInt(5)+1;
                for( int k = 0; k < grandChildren; k++) {
                    NLevelItem child = new NLevelItem(new SomeObject("child "+k),parent, new NLevelView() {
                        @Override
                        public View getView(NLevelItem item) {
                            View view = inflater.inflate(R.layout.contact_item, null);
                            TextView tv = (TextView) view.findViewById(R.id.textView);
                            tv.setBackgroundColor(Color.GRAY);
                            String name = (String) ((SomeObject) item.getWrappedObject()).getName();
                            tv.setText(name);
                            return view;
                        }
                    });
                    list.add(child);
                }
            }
        }

        NLevelAdapter adapter = new NLevelAdapter(list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                ((NLevelAdapter)listView.getAdapter()).toggle(arg2);
                ((NLevelAdapter)listView.getAdapter()).getFilter().filter();

            }
        });

    }

    @Override
    public void onSuccess(boolean isSuccess, String msg, String data) {
        GroupModel groupList = JSON.parseObject(
                data, GroupModel.class);
    }

    @Override
    public void onFailed(HttpException error, String msg) {

    }

    class SomeObject {
        public String name;

        public SomeObject(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
    }
    private void webRequest(int groupid){
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("groupid",groupid+"");
        WebUtils.sendPost(this,"groupDepList",map,this);
    }
}
